#!/bin/bash
# Copyright VMware, Inc.
# SPDX-License-Identifier: APACHE-2.0

# shellcheck disable=SC1091

set -o errexit
set -o nounset
set -o pipefail
# set -o xtrace # Uncomment this line for debugging purposes

# Load libraries
. /opt/bitnami/scripts/libjenkins.sh
. /opt/bitnami/scripts/liblog.sh

# Load Jenkins environment
. /opt/bitnami/scripts/jenkins-env.sh

declare -a args
if [[ -n "${JAVA_OPTS:-}" ]]; then
    read -r -a java_opts <<<"$JAVA_OPTS"
    args+=("${java_opts[@]}")
fi

args+=("-Duser.home=${NACOS_HOME}" "-jar" "${NACOS_HOME}/target/nacos-server.jar")

if [[ -n "${NACOS_OPTS:-}" ]]; then
    read -r -a nacos_opts <<<"$NACOS_OPTS"
    args+=("${nacos_opts[@]}")
fi
args+=("$@")

info "** Starting Nacos **"
if am_i_root; then
    exec_as_user "$NACOS_DAEMON_USER" java "${args[@]}"
else
    exec java "${args[@]}"
fi
